<template>
    <view class="content">
        <image class="logo" src="../../static/logo.png" mode="aspectFill"></image>
    </view>
</template>

<script>
    import { getUserOpenId } from "../../utils/getOpenId";
    import { md5 } from 'js-md5';
    import { getUserScoreFromCloud, saveUserCloudScoreToLocal } from '../../store/scoreManager.js'

    export default {
        data() {
            return {
                title: 'Hello',
                usertype: 1
            };
        },
        onShow: function (e) {
            // 先判断有无注册过用户名 如果注册过则为正常模式 
            // 正常模式userType = 2
            const username = uni.getStorageSync("kUsername");
            // 判断有无有户名 
            if (username && username.length > 0) {
                uni.setStorageSync("userType", 2);
                this.usertype = 2;
                setTimeout(() => {
                    uni.reLaunch({
                        url: '/pages/index/shouye' + '?usertype=' + this.usertype
                    });
                }, 800);
            }
            // 如果userType!=1 则提示注册用户
            this.usertype = 1;
            uni.setStorageSync("userType", 1);

            let appVersion = "2.0.19";
            const db = uniCloud.database();
            if (appVersion) {
                db.collection("open-key").get().then((res) => {
                    // 审核模式
                    if (res.result.data && res.result.data.length > 0) {
                        if (res.result.data[0].version == appVersion) {
                            uni.setStorageSync("userType", 1);
                            this.usertype = 1;
                            uni.reLaunch({
                                url: '/pages/index/shouye' + '?usertype=' + this.usertype
                            });
                        } else {
                            uni.setStorageSync("isNewUser", res.result.data[0].isNewUser);
                            uni.setStorageSync("userType", 2);
                            this.usertype = 2;
                            this.loginUser();
                        }
                    } else {
                        uni.setStorageSync("userType", 1);
                        this.usertype = 1;
                        uni.reLaunch({
                            url: '/pages/index/shouye' + '?usertype=' + this.usertype
                        });
                    }
                }).catch((e) => {
                    uni.setStorageSync("userType", 1);
                    this.usertype = 1;
                    uni.reLaunch({
                        url: '/pages/index/shouye' + '?usertype=' + this.usertype
                    });
                });
            }
        },
        methods: {
            loginUser() {
                getUserOpenId().then((openid) => {
                    const username = openid;
                    const db = uniCloud.database();
                    db.collection('open_user').where({
                        username: username
                    }).field("_id,username,score").get().then((queryUser) => {
                        uni.setStorageSync("userid", queryUser._id);
                        if (queryUser.result.data && queryUser.result.data.length > 0) { // 已注册
                            uni.reLaunch({
                                url: '/pages/index/shouye' + '?usertype=' + this.usertype
                            });
                        } else { // 未注册用户
                            uni.navigateTo({
                                url: '/pages/register/register'
                            });
                        }
                    }).catch((e) => {
                        uni.setStorageSync("userType", 1);
                        this.usertype = 1;
                        uni.reLaunch({
                            url: '/pages/index/shouye' + '?usertype=' + this.usertype
                        });
                    });
                }).catch((e) => {
                    uni.setStorageSync("userType", 1);
                    this.usertype = 1;
                    uni.reLaunch({
                        url: '/pages/index/shouye' + '?usertype=' + this.usertype
                    });
                });
            }
        }
    };
</script>

<style>
   .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

   .text-area {
        display: flex;
        justify-content: center;
        color: gold;
        font-size: 100rpx;
        font-weight: bold;
        width: 100%;
        text-align: center;
    }

   .logo {
        height: 250rpx;
        width: 250rpx;
        margin-top: 30vh;
        border-radius: 50%;
    }
</style>